<template>
  <el-dialog
    :title="isEait ? '修改部门':'添加部门'"
    :visible.sync="showDialog"
    width="30%"
    :before-close="handleClose"
  >
    <el-form ref="formData" :model="formData" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" placeholder="请选择负责人">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartment, getManagerList, addDepartment, getDetail, updateDepartment } from '@/api/department'
export default {
  props: {
    // 弹出   关闭  接收父传来的值
    showDialog: {
      type: Boolean,
      required: true
    },
    curNodeId: {
      type: [String, Number],
      required: true
    },
    isEait: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      managerList: [],
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人
        pid: ''
      },
      // 表单验证
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            // 发送请求
            const res = await getDepartment()
            let data = res.data
            // 1.修改部门时  先从data中排除自己
            if (this.isEait) {
              data = res.data.filter(item => item.id !== this.formData.id)
            }

            // 2.添加部门时  判断  当前的部门名字是 value  如果它已经存在,就提示被占用了
            if (data.find(item => item.name === value)) {
              callback(new Error('这个名称[' + value + ']已被占用'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            // 发送请求
            const res = await getDepartment()
            let data = res.data
            // 1.修改部门时  先从data中排除自己
            if (this.isEait) {
              data = res.data.filter(item => item.id !== this.formData.id)
            }
            if (data.find(item => item.code === value)) {
              callback(new Error('编码已存在'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '不能为空', trigger: 'blur' }
          // { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    // 获取部门详情
    async getDetail() {
      const res = await getDetail(this.curNodeId)
      // console.log(res)
      // 数据回显
      this.formData = res.data
    },
    // 获取部门负责人
    async getManagerList() {
      const res = await getManagerList()
      this.managerList = res.data
    },
    // 添加部门
    async addDepartment() {
      await addDepartment({ ...this.formData, pid: this.curNodeId })
      this.$message.success('添加成功')
      // 关闭  清空表单
      this.handleClose()
      this.$emit('updatadep')
    },
    // 修改部门
    async updateDepartment() {
      await updateDepartment(this.formData)
      this.$message.success('修改成功')
      // 关闭  清空表单
      this.handleClose()
      this.$emit('updatadep')
    },
    onSubmit() {
      if (this.isEait) {
        // 修改部门
        this.$refs.formData.validate(valid => {
          valid && this.updateDepartment()
        })
      }
      // 添加部门
      this.$refs.formData.validate(valid => {
        valid && this.addDepartment()
      })
    },
    // 表单关闭事件   :close
    handleClose() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人
        pid: ''
      }
      // this.$refs.formData.resetFields()
      // 事件处理:子传父时: this.$emit('update:处理的事件',传入处理的值
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
